﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ComputerStoreManager {
    /// <summary>
    /// Interaction logic for LoginPanel.xaml
    /// </summary>
    public partial class LoginPanel : Window {
        public static string MaNhanVien { get; set; }
        private static LoginPanel _instance;
        private static object syncLock = new object();

        public static LoginPanel GetInstance() {
            if (_instance == null) {
                lock (syncLock) {
                    if (_instance == null) {
                        _instance = new LoginPanel();
                    }
                }
            }
            return _instance;
        }

        public LoginPanel() {
            string procName = Process.GetCurrentProcess().ProcessName;

            // get the list of all processes by the "procName"       
            Process[] processes = Process.GetProcessesByName(procName);

            if (processes.Length > 1) {
                MessageBox.Show(procName + " already running");
                this.Close();
                return;
            }
            else {
                InitializeComponent();
            }

            Closed +=LoginPanel_Closed;

        }


        void LoginPanel_Closed(object sender, EventArgs e) {
            this.Close();
            Process.GetCurrentProcess().Kill();

        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            //exit
            this.Close();
            Process.GetCurrentProcess().Kill();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e) {
            //log in
            string user = this.username.Text;
            string pass = this.pass.Password;
            string check = Model.CheckLogin(user, pass);
            if (check != null) {
                MessageBox.Show("Log in thành công");
                MaNhanVien = check;
                this.Hide();
                MainWindow main = MainWindow.GetInstance();
                //main.OnMainWinDowExitClicked += main_OnMainWinDowExitClicked;
                main.Show();
            }
            else {
                MessageBox.Show("Log in thất bại");
            }

        }

        //void main_OnMainWinDowExitClicked() {
        //    //this.Visibility = Visibility.Visible;
        //}
    }
}
